<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">
		<title>公告管理</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<link rel="stylesheet" type="text/css" media="screen, projection, print" href="<%=path%>/ext-3.4.0/resources/css/ext-all.css" />
		<script type="text/javascript" src="<%=path%>/ext-3.4.0/adapter/ext/ext-base.js"></script>
		<script type="text/javascript" src="<%=path%>/ext-3.4.0/ext-all.js"></script>
		<script type="text/javascript" src="<%=path%>/js/jquery-1.7.1.min.js"></script>
		<script type="text/javascript" src="<%=request.getContextPath()%>/fckeditor/fckeditor.js"></script>
		<style type="text/css">
		html,body{
			height:100%;
		}
		</style>
	</head>
	<body>
		<script type="text/javascript">
		function SetEditorContents(EditorName, ContentStr){
			var oEditor = FCKeditorAPI.GetInstance(EditorName);
			oEditor.SetHTML(ContentStr);
		}
		Ext.onReady(function () {
	
			Ext.BLANK_IMAGE_URL = "<%=path%>/ext-3.4.0/resources/images/default/s.gif";
			Ext.QuickTips.init();
			Ext.form.Field.prototype.msgTarget = "side";
		
			function modfiy() {
				if (grid.getSelectionModel().hasSelection()) {
					var records = grid.getSelectionModel().getSelections();
					var mycars = new Array();
					for (var i = 0; i < records.length; i++) {
						mycars[i] = records[i].data.id;
					}
					return mycars
				} else {
					top.Ext.MessageBox.alert("提示", "请选中要操作的记录!");
					return false;
				}
			};
		
			function sendCommend(url) {
				Ext.Ajax.request({
					url : url,
					method : 'post',
					success : function (response, options) {
						var result = Ext.util.JSON.decode(response.responseText);
						if (result.success) {
							//执行成功
							store.reload();
						}
						top.Ext.Msg.alert("提示", result.message);
					},
					failure : function () {
						top.Ext.Msg.alert("提示", "请求发送未成功。");
						return false;
					}
				});
			}
			
			function loadNewsData(url){
				var _tUrl_ = url;
				Ext.Ajax.request({
					url : url,
					method : 'post',
					success : function (response, options) {
						var result = response.responseText;
						var title = result.split("&tt&")[0];
						var body = result.split("&tt&")[1];
						var id = result.split("&tt&")[2];
						//设置ID，和标题
						Ext.getCmp('title').setValue(title);
						Ext.getCmp('id').setValue(id);
						//设置内容，使用FCK API
						SetEditorContents("body",body);
					},
					failure : function () {
						top.Ext.Msg.alert("提示", "请求发送未成功。");
						return false;
					}
				});
			}
		
			var createOrUpdate = function (id) {
				var addTypeFormPanel = new Ext.form.FormPanel({
						labelSeparator : ':',
						labelWidth : 60,
						border : false,
						bodyStyle : 'padding:0 0 0 0',
						frame : true,
						fileUpload : true, 
						height : 'auto',
						width : 700,
						items : [
							new Ext.form.Hidden({
								id:'id',
								name : 'id'
							}),
							new Ext.form.TextField({
								name : 'title',
								id:'title',
								width : 600,
								fieldLabel : '标题',
								allowBlank : false,
								emptyText : "请输入新闻标题",
								blankText : '新闻标题不能为空'
							}), {
						      xtype:"textarea",   
						      fieldLabel:"编辑",   
						      labelSeparator:":",
						      id:"body",
						      width:600,
						      height:371
						   }
						]
					})
					var addTypeWindow = new Ext.Window({
						title : '发布新闻',
						width : 750,
						height : 500,
						layout : 'fit',
						plain : true,
						resizable : false,
						closable : true,
						bodyStyle : 'padding:5px;',
						buttonAlign : 'center',
						modal : true,
						items : addTypeFormPanel,
						buttons : [{
								text : "确定",
								handler : function () {
									if (addTypeFormPanel.form.isValid()) {
										addTypeFormPanel.form.submit({
											url : "<%=path%>/manager.do?method=NewCreateOrUpdate",
											method : "post",
											success : function (form, action) {
												store.reload();
												addTypeWindow.destroy();
												top.Ext.MessageBox.alert("提示", "操作成功，您的数据已经被系统受理。");
											},
											failure : function (form, action) {
												addTypeWindow.destroy();
												top.Ext.MessageBox.alert("提示", "操作失败,请检查数据是否填写完整?");
											}
										})
									} else {
										top.Ext.MessageBox.alert('提示', '提交失败,请检查您的信息是否填写完整?');
									}
								}
							}, {
								text : '取消',
								handler : function () {
									addTypeWindow.destroy();
								}
							}
						]
					});
				addTypeWindow.show();
				/*创建Fckeditor对象 -- 构造函数指出了Fckeditor要挂接的id,上文中的xtype : 'textarea'指定*/  
				//在页面上要求挂载fckeditor/fckeditor.js文件   
				oFCKeditor = new FCKeditor("body",600,371);
				oFCKeditor.BasePath = "<%=request.getContextPath()%>/fckeditor/";
				oFCKeditor.ToolbarSet = 'Article';
				oFCKeditor.ReplaceTextarea();
				
				if(id != undefined){
					var _u_ = "manager.do?method=findNewsById&id="+id;
					loadNewsData(_u_);
				}
				
			}
	
			/******************************
			 *         定义表格组件         *
			 ******************************/
	
			//定义菜单选择项
			var toolbar = new Ext.Toolbar({
				width : '100%',
				border : false
			});
			toolbar.add(
			new Ext.Button({
				text : '+ 添加新闻',
				handler : function () {
					createOrUpdate();
				}
			}),
			'-',
			new Ext.Button({
				text : '- 删除新闻',
				handler : function () {
					var ids = modfiy();
					if (ids) {
						var url = "<%=path%>/manager.do?method=NewsDelete&ids=" + ids.toString();
						sendCommend(url);
					}
				}
			}))
	
			var store = new Ext.data.Store({
				reader : new Ext.data.JsonReader({
					root : 'items',
					totalProperty : 'results',
					fields : [{
							name : 'id'
						}, {
							name : "title"
						}, {
							name : "updatetime"
						},{
							name:'xxx'
						}
					]
				}),
				proxy : new Ext.data.HttpProxy({
					url : '<%=path%>/manager.do?method=getNewsList'
				})
			})
	
			var sm = new Ext.grid.CheckboxSelectionModel(); //定义选择模式
			var grid = new Ext.grid.GridPanel({
				title : "新闻管理",
				height : '100%',
				frame : true,
				stripeRows : true,
				tortable : true,
				loadMask : {
					msg : '正在加载数据，请稍侯...'
				},
				tbar : [toolbar],
				sm : sm,
				store : store,
				viewConfig : {
					columnsText : '显示列',
					sortAscText : '升序',
					sortDescText : '降序'
				},
				listeners : {
					dblclick : function () {
						var ids = modfiy();
						if (ids.length == 1) {
							//编辑操作
							//Ext.Msg.alert("提示","编辑操作。");
							createOrUpdate(ids.toString());
						} else {
							top.Ext.Msg.alert("提示", "请选择一条记录进行编辑。");
							return false;
						}
					}
				},
				bbar : new Ext.PagingToolbar({
					pageSize : 25,
					store : store,
					beforePageText : "当前第",
					afterPageText : "页，共{0}页",
					lastText : "尾页",
					nextText : "下一页",
					prevText : "上一页",
					firstText : "首页",
					refreshText : "刷新页面",
					displayInfo : true,
					displayMsg : "当前显示 {0} - {1}条, 共 {2}"
				}),
				columns : [
					sm, {
						header : '标题',
						width : 400,
						dataIdex : 'resName',
						sortabel : true
					}, {
						header : '发布时间',
						width : 200,
						dataIdex : 'eduType',
						sortabel : true,
						renderer : function (value, cellmeta, record, rowIndex, columnIndex, color_store) {
							if (value != null) {
								return uptime = new Date(value.time).format('Y-m-d');
							} else {
								return null;
							}
						}
					}
				]
			});
	
			//主面板
			new Ext.Viewport({
				layout : 'border',
				bodyBorder : false,
				border : false,
				items : [{
						title : '',
						collapsible : false,
						region : 'center',
						layout : 'fit',
						items : [grid]
					}
				]
			});
			store.load();
		})
		</script>
	</body>
</html>
